﻿#include "HoverButton.h"

HoverButton::HoverButton(QWidget *parent)
    : QWidget(parent), m_collapsedWidth(40), m_expandedWidth(200), m_isExpanded(false) {

    // 设置样式和初始大小
    setFixedWidth(m_collapsedWidth);
    setStyleSheet("background-color: #f0f0f0; border-right: 1px solid #cccccc;");

    // 创建布局和按钮
    m_layout = new QVBoxLayout(this);
    m_layout->setSpacing(5);
    m_layout->setContentsMargins(5, 5, 5, 5);

    // 添加示例按钮
    addButton("单张识别", QIcon::fromTheme("document-open"));
    addButton("多张识别", QIcon::fromTheme("document-open"));

    // 添加弹簧使按钮顶部对齐
    m_layout->addStretch();

    // 初始化动画
    m_animation = new QPropertyAnimation(this, "expandWidth");
    m_animation->setDuration(300); // 300毫秒动画
    m_animation->setEasingCurve(QEasingCurve::OutCubic);

    // 初始化延迟计时器
    m_collapseTimer = new QTimer(this);
    m_collapseTimer->setSingleShot(true);
    connect(m_collapseTimer, &QTimer::timeout, this, &HoverButton::collapse);

    // 设置鼠标跟踪
    setMouseTracking(true);
}

int HoverButton::expandWidth() const { return width(); }

void HoverButton::setExpandWidth(int width) { setFixedWidth(width); }

void HoverButton::addButton(const QString &text, const QIcon &icon) {
    QPushButton *button = new QPushButton(icon, "", this);
    button->setText(text);
    button->setIconSize(QSize(16, 16));
    button->setStyleSheet(
        "QPushButton {"
        "   text-align: left;"
        "   padding: 5px;"
        "   border: none;"
        "   background-color: transparent;"
        "}"
        "QPushButton:hover {"
        "   background-color: #e0e0e0;"
        "   border-radius: 4px;"
        "}"
        );

    // 初始状态下只显示图标
    button->setToolTip(text);
    button->setFixedHeight(30);

    m_layout->insertWidget(m_layout->count() - 1, button); // 插入到弹簧之前
    m_buttons.append(button);
}

void HoverButton::enterEvent(QEnterEvent *event) {
    Q_UNUSED(event)
    m_collapseTimer->stop(); // 取消延迟收起
    expand();
}

void HoverButton::leaveEvent(QEvent *event) {
    Q_UNUSED(event)
    // 延迟300毫秒后收起，防止误操作
    m_collapseTimer->start(100);
}

void HoverButton::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event)
    QPainter painter(this);
    painter.fillRect(rect(), QColor(240, 240, 240));

    // 绘制左侧边框
    painter.setPen(QColor(200, 200, 200));
    painter.drawLine(rect().topRight(), rect().bottomRight());
}

void HoverButton::expand() {
    if (m_isExpanded) return;

    m_animation->stop();
    m_animation->setStartValue(width());
    m_animation->setEndValue(m_expandedWidth);
    m_animation->start();

    // 展开时显示按钮文本
    for (QPushButton *button : m_buttons) {
        button->setText(button->toolTip());
    }

    m_isExpanded = true;
}

void HoverButton::collapse() {
    if (!m_isExpanded) return;

    m_animation->stop();
    m_animation->setStartValue(width());
    m_animation->setEndValue(m_collapsedWidth);
    m_animation->start();

    // 收起时隐藏按钮文本
    for (QPushButton *button : m_buttons) {
        button->setText("");
    }

    m_isExpanded = false;
}
